<template>
  <div class="app-container org">
    <el-card class="leftCard">
      <!-- 搜索条件盒子 -->
      <div>
        <p class="pinformation">搜索条件</p>
        <el-form :model="queryParams" ref="queryRef" @submit.native.prevent>
          <el-row>
            <el-col :span="15">
              <el-form-item label="单位名称：" prop="unitName">
                <el-input
                  v-model="queryParams.unitName"
                  placeholder="请输入计量单位名称"
                  maxlength="40"
                  clearable
                  @keyup.enter="handleQuery"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8" :offset="1">
              <el-row justify="end">
                <el-button type="success" @click="handleQuery">搜索</el-button>
                <el-button class="business resetBtn" @click="resetQuery"
                  >重置</el-button
                >
              </el-row>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div>
        <el-row>
          <p class="pinformation">计量单位库列表</p>
          <p class="totalStyle">共 {{ total }} 条数据</p>
          <el-row style="padding-bottom: 5px;margin-left: 15px;">
            <!-- <el-button
              link
              type="success"
              icon="Refresh"
              @click="handleSynchronous"
              :loading="loadingBtn"
              >同步数据</el-button
            > -->
          </el-row>
        </el-row>

        <el-table
          ref="mainTableRef"
          class="business"
          v-loading="loading"
          :data="unitTable"
          :row-class-name="tableRowClass"
          @row-click="rowClick"
        >
          <el-table-column label="计量单位名称" align="center" prop="unitName">
            <template #default="scope">
              <span v-html="transformation(scope.row.unitName)"></span>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </el-card>

    <el-card class="rightCard">
      <!-- 计量单位 -->
      <div>
        <el-row style="margin-bottom: 5px">
          <el-col :span="22">
            <p class="pinformation">计量单位</p>
          </el-col>
        </el-row>
        <el-form label-width="130px" :model="form" ref="unitRef">
          <el-row>
            <el-col :span="14">
              <el-form-item label="计量单位名称：" prop="unitName">
                <span
                  v-html="transformation(form.unitName)"
                  v-if="form.unitName"
                ></span>
                <span v-else>/</span>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="计量单位组别：" prop="unitGroup">
                <dict-tag
                  :options="lims_unit_type"
                  :value="form.unitGroup"
                  v-if="form.unitGroup >= 0"
                />
                <div v-else>/</div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-col :span="10">
            <el-form-item label="计量单位中文名称：" prop="chineseName">
              {{ form.chineseName || "/" }}
            </el-form-item>
          </el-col>
        </el-form>
      </div>

      <!-- 计量单位换算子表 -->
      <div>
        <div>
          <el-row>
            <p class="pinformation">计量单位换算</p>
            <p class="totalStyle">
              共 {{ form.basicUnitParseList.length }} 条数据
            </p>
          </el-row>
        </div>
        <el-table
          :data="form.basicUnitParseList"
          ref="subTableRef"
          v-loading="basicUnitParseLoading"
        >
          <el-table-column label="计量单位" prop="unitName" align="center" />
          <el-table-column label="换算数量" prop="times" align="center" />
          <el-table-column
            label="换算计量单位"
            align="center"
            prop="unitItmesName"
          >
          </el-table-column>
        </el-table>
      </div>
    </el-card>
  </div>
</template>

<script setup name="Unit">
import { listUnit, getUnit, getUnitList, getUnitStatus, postUnit } from "@/api/store/unit";

import { ElMessageBox } from "element-plus";

const { proxy } = getCurrentInstance();

const { lims_unit_type } = proxy.useShareDict("lims_unit_type");

const unitList = ref([]);
const basicUnitParseLoading = ref(false);
const basicUnitParseForm = ref({});

const loading = ref(true);
// const loadingBtn = ref(false)
const total = ref(0);
const unitTable = ref([]);
const heightBright = ref("");

const data = reactive({
  form: {
    basicUnitParseList: [],
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    unitName: null,
    unitGroup: null,
  },
});

const { queryParams, form } = toRefs(data);

/** 查询计量单位库列表 */
function getList() {
  loading.value = true;
  listUnit(queryParams.value).then((response) => {
    unitTable.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 表单重置
function reset() {
  form.value = {};
  basicUnitParseForm.value = {};
  form.value.basicUnitParseList = [];
  proxy.$refs["unitRef"].clearValidate();
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 修改按钮操作 */
function rowClick(row) {
  reset();
  getUnit(row.unitId).then((response) => {
    form.value = response.data;
    if (!form.value.basicUnitParseList) form.value.basicUnitParseList = [];
    heightBright.value = row.unitId;
    tableRowClass({ row });
  });
}

function getJldwList() {
  getUnitList().then((res) => {
    unitList.value = res.data;
  });
}

getJldwList();
getList();

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [
        { tableName: "main", bottomHeight: 74 },
        { tableName: "sub", bottomHeight: 24 },
      ];
    } else {
      heightParams.value = [
        { tableName: "main", bottomHeight: 21 },
        { tableName: "sub", bottomHeight: 24 },
      ];
    }
    getTableHeight();
  }
);

const heightParams = ref([
  { tableName: "main", bottomHeight: 21 },
  { tableName: "sub", bottomHeight: 21 },
]);
function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

onMounted(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
})

onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

function transformation(name) {
  const a = proxy.getSupAndSub(name);
  return a;
}

function tableRowClass({ row }) {
  if (row) {
    if (row.unitId == heightBright.value) {
      return "current-row";
    }
    return "";
  }
}

// function handleSynchronous() {
//   proxy.$modal
//     .confirm("确认要同步数据吗？")
//     .then(() => {
//       postUnit();
//       loadingBtn.value = true;
//       let timer = setInterval(() => {
//         getUnitStatus()
//           .then((res) => {
//             if (res.data == 2) {
//               clearInterval(timer);
//               loadingBtn.value = false;
//               getList();
//               ElMessageBox.alert("同步成功", "提示", {
//                 type: "success",
//               });
//             }
//           })
//           .catch(() => {
//             clearInterval(timer);
//             loadingBtn.value = false;
//             ElMessageBox.alert("同步失败", "提示", {
//               type: "error",
//             });
//           });
//       }, 5000);
//     })
//     .catch(() => {});
// }
</script>

<style>
.xpsPrimaryBtn {
  background-color: #fff !important;
}
</style>
